package com.c0der.neat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Node {
    private double value = 0;
    private List<Connection> connections = new ArrayList<>();

    public Node() {
    }

    private boolean flag = false;

    public synchronized double getValue(Map<Integer, Node> nodes) {
        if (!flag && connections.size() > 0) {
            flag = true;
            for (Connection connection : connections) {
                int from = connection.getFrom();
                Node node = nodes.get(from);
                double v = node.getValue(nodes);
                value += v * connection.getWeight() + connection.getBias();
            }
            value = ActiveFunction.sigmod.active(value);
            flag = false;
        }
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public void addFrom(Connection connection) {
        connections.add(connection);
    }

    public List<Connection> getFrom() {
        return connections;
    }
}
